package com.xique.door.controller;

import com.xique.common.core.domain.R;
import com.xique.common.core.utils.StringUtils;
import com.xique.common.core.utils.poi.ExcelUtil;
import com.xique.common.core.web.controller.BaseController;
import com.xique.common.core.web.domain.AjaxResult;
import com.xique.common.core.web.page.TableDataInfo;
import com.xique.common.log.annotation.Log;
import com.xique.common.log.enums.BusinessType;
import com.xique.common.security.annotation.PreAuthorize;
import com.xique.door.api.model.LoginAppUser;
import com.xique.door.bean.domain.BaseUser;
import com.xique.door.bean.domain.User;
import com.xique.door.biz.service.IBaseUserBizService;
import com.xique.door.service.IBaseUserService;
import com.xique.door.service.IUserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 【请填写功能名称】Controller
 *
 * @author xique
 * @date 2021-12-10
 */
@RestController
@RequestMapping("/base/user")
public class BaseUserController extends BaseController
{
    @Autowired
    private IBaseUserService baseUserService;

    @Autowired
    private IBaseUserBizService baseUserBizService;

    /**
     * 查询列表
     */
//    @PreAuthorize(hasPermi = "system:user:list")
    @GetMapping("/list")
    public TableDataInfo list(BaseUser cBaseUser)
    {
        startPage();
        List<BaseUser> list = baseUserService.selectBaseUserList(cBaseUser);
        return getDataTable(list);
    }

    /**
     * 导出列表
     */
    @PreAuthorize(hasPermi = "system:user:export")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BaseUser cBaseUser) throws IOException
    {
        List<BaseUser> list = baseUserService.selectBaseUserList(cBaseUser);
        ExcelUtil<BaseUser> util = new ExcelUtil<BaseUser>(BaseUser.class);
        util.exportExcel(response, list, "【请填写功能名称】数据");
    }

    /**
     * 获取详细信息
     */
    @PreAuthorize(hasPermi = "system:user:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(baseUserService.selectBaseUserById(id));
    }

    /**
     * 新增
     */
    @PreAuthorize(hasPermi = "system:user:add")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BaseUser cBaseUser)
    {cBaseUser.setRemark("BaseUserController_add");
        return toAjax(baseUserService.insertBaseUser(cBaseUser));
    }

    /**
     * 修改
     */
    @PreAuthorize(hasPermi = "system:user:edit")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BaseUser cBaseUser)
    {
        return toAjax(baseUserService.updateBaseUser(cBaseUser));
    }

    /**
     * 删除
     */
    @PreAuthorize(hasPermi = "system:user:remove")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(baseUserService.deleteBaseUserByIds(ids));
    }

    /**
     * 获取当前用户信息
     */
    @GetMapping("/info/{loginName}")
    @ApiOperation("登录时获取当前用户信息")
    public R<LoginAppUser> info(@PathVariable("loginName") String loginName)
    {
        return baseUserBizService.getAppUserByLoginName(loginName);
    }

    @GetMapping("/getGzhUserInfo")
    public AjaxResult getGzhUserInfo(String openId) {
        return baseUserBizService.getGzhUserInfo(openId);
    }

    @GetMapping("/v1/getGzhUserInfo")
    public AjaxResult getGzhUserInfoV1() {
        return baseUserBizService.getGzhUserInfoV1();
    }



    /**===================================用户数据处理======================================*/
    @Autowired
    private IUserService userService;

    @GetMapping("/dealBaseUserData")
    public AjaxResult dealBaseUserData() {

        List<User> list = userService.selectUserListWithoutBaseUser();

        for (User user : list) {
            String phone = StringUtils.trim(user.getPhone());
            if (StringUtils.isNotBlank(phone)) {
                BaseUser baseUser = baseUserService.selectBaseUserByLoginName(phone);

                if (StringUtils.isNull(baseUser)) {
                    baseUser = new BaseUser();
                    baseUser.setUserName(user.getUserName());
                    baseUser.setCellphone(user.getPhone());
                    baseUser.setFaceUrl(user.getFaceUrl());
                    baseUser.setFaceKey(user.getFaceKey());
                    baseUser.setAddress(user.getAddress());
                    baseUser.setRemark(user.getRemark());
                    baseUser.setCreateTime(user.getCreateTime());
                    baseUser.setCreateUser(user.getCreateUser());
                    baseUser.setUpdateTime(user.getUpdateTime());
                    baseUser.setUpdateUser(user.getUpdateUser());
                    baseUser.setRemark("用户数据处理,有电话号码");
                    baseUserService.insertBaseUser(baseUser);
                }

                User updateUser = new User();
                updateUser.setId(user.getId());
                updateUser.setBaseUserId(baseUser.getId());
                user.setUserType(0);
                userService.updateUser(updateUser);
            } else {
                BaseUser baseUser = new BaseUser();
                baseUser.setUserName(user.getUserName());
                baseUser.setCellphone(user.getPhone());
                baseUser.setFaceUrl(user.getFaceUrl());
                baseUser.setFaceKey(user.getFaceKey());
                baseUser.setAddress(user.getAddress());
                baseUser.setRemark(user.getRemark());
                baseUser.setCreateTime(user.getCreateTime());
                baseUser.setCreateUser(user.getCreateUser());
                baseUser.setUpdateTime(user.getUpdateTime());
                baseUser.setUpdateUser(user.getUpdateUser());
                baseUser.setRemark("用户数据处理,没有电话号码");
                baseUserService.insertBaseUser(baseUser);

                User updateUser = new User();
                updateUser.setId(user.getId());
                updateUser.setBaseUserId(baseUser.getId());
                user.setUserType(0);
                userService.updateUser(updateUser);

            }
        }

        return null;
    }
}
